# coding=utf-8

import re
import os

def FindIncludeFile(FileName, FindDirs):
    for d in FindDirs:
        FilePath = os.path.join(d, FileName)
        FilePath = os.path.abspath(FilePath)
        if os.path.isfile(FilePath):
            return FilePath
    return None

def FindInclude(FilePath, SysDirs, depth = 0, CheckedFiles = None):
    assert os.path.isfile(FilePath)
    if not CheckedFiles:
        CheckedFiles = set()

    if FilePath in CheckedFiles:
        print("%s[%s]" % ('  ' * depth, FilePath))
        return

    CheckedFiles.add(FilePath)

    BASE_DIR = os.path.dirname(FilePath)
    BASE_NAME = os.path.basename(FilePath)
    # print(BASE_DIR)
    # print(BASE_NAME)
# 查找 Include
    PATTERN_USER = '^#include "(.*?)"'
    regx_user = re.compile(PATTERN_USER)
    PATTERN_SYS = '^#include <(.*?)>'
    regx_sys = re.compile(PATTERN_SYS)

    with open(FilePath, encoding="utf-8") as f:
        print("%s%s" % ('  ' * depth, FilePath))
        for r in f:
            # print(r)
            m = regx_user.search(r)
            if m:
                # print(m.group())
                FileName = m.group(1)
                FindDirs = [BASE_DIR] + SysDirs
                IncludeFile = FindIncludeFile(FileName, FindDirs)
                if not IncludeFile:
                    print("Include file %s not found!" % FileName)
                    exit(1)
                FindInclude(IncludeFile, SysDirs, depth + 1, CheckedFiles)
            # if not m:
            #     m = regx_sys.search(r)
            #     if m:
            #         print(m.group())
            #         FileName = m.group(1)
            #         IncludeFile = FindIncludeFile(FileName, SysDirs)
            #         if not IncludeFile:
            #             print("Include file %s not found!" % FileName)
            #             exit(1)
            #         FindInclude(IncludeFile, SysDirs)
            pass
    
def test1():
    FilePath = r"d:\work\dev\static_lib_d0\build_MD_LOG\microbenchlib\PrimSetup_gll.cpp"
    # SysDirs = [
    #     r"D:\work\dev\static_lib_d0\build_MD_LOG\microbenchlib",
    #     r"D:\work\dev\static_lib_d0\tests",
    #     r"D:\work\dev\static_lib_d0\microbenchlib",
    # ]

    SysDirs = [
        r"d:\dev\p4\microbench_dk\win\ms_sdk\n17713\10\Include\10.0.17713.0\shared",
        r"d:\dev\p4\microbench_dk\win\ms_sdk\n17713\10\Include\10.0.17713.0\ucrt",
        r"d:\dev\p4\microbench_dk\win\ms_sdk\n17713\10\Include\10.0.17713.0\um",
        r"d:\dev\p4\microbench_dk\win\ms_sdk\n17713\10\Include\10.0.17713.0\winrt",
        r"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include",
        r"D:\work\dev\static_lib_d0\build_MD_LOG\microbenchlib",
        r"D:\work\dev\static_lib_d0\build_MD_LOG\microbenchlib\include",
        r"D:\work\dev\static_lib_d0\tests",
        r"D:\work\dev\static_lib_d0\tests\PrimSetup",
        r"D:\work\dev\static_lib_d0\microbenchlib",
    ]

    FindInclude(FilePath, SysDirs)

def main():
    test1()
    pass

if __name__ == "__main__":
    main()
